package com.cloud.force.api.web;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.cloud.force.api.bean.MenuTabAuthority;
import com.cloud.force.api.bean.Part;
import com.cloud.force.api.bean.ProcDef;
import com.cloud.force.api.entity.TaskListMenu;
import com.cloud.force.api.entity.TaskListMenuGroup;
import com.cloud.force.api.service.TaskListMenuService;
import com.cloud.force.common.result.Quokka;
import com.cloud.force.interfaces.FeignClientFreeflow;

@RestController
@RequestMapping("force/api/tasklist/menu")
public class TaskListMenuController {

	@Autowired
	private TaskListMenuService taskListMenuService;

	@Autowired
	private FeignClientFreeflow feignClientFreeflow;

	@GetMapping("/listtree")
	public Quokka listTree() {
		return new Quokka(true, taskListMenuService.listTree());
	}

	@RequestMapping("/groups")
	public Quokka listGroups() {
		return new Quokka(true, taskListMenuService.listTaskListMenuHeader());
	}

	@RequestMapping("/listauthority")
	public Quokka listPartAuthority(long taskListId, long actDefId) {
		List<Part> parts = feignClientFreeflow.loadPartsByActDef(actDefId);
		return new Quokka(true, taskListMenuService.listPartAuthority(taskListId, actDefId, parts));
	}

	@RequestMapping("/saveupdate")
	public Quokka saveUpdatePartAuth(@RequestBody List<MenuTabAuthority> parts, long actDefId, long taskListId) {
		taskListMenuService.saveUpdatePartAuth(parts, actDefId, taskListId);
		return new Quokka(true);
	}

	@RequestMapping("/listbypart")
	public Quokka listTaskListMenuPart(long taskListId, long actDefId) {
		List<Long> parts = new ArrayList<Long>();
		parts.add(0L);
		return new Quokka(true, taskListMenuService.listTaskListMenuByPart(taskListId, actDefId, parts));
	}

	@PostMapping("/group/saveupdate")
	public Quokka saveUpdateGroup(@RequestBody TaskListMenuGroup group) {
		return new Quokka(true, taskListMenuService.saveUpdateGroup(group));
	}

	@GetMapping("/group/delete")
	public Quokka deleteGroup(long groupId) {
		taskListMenuService.deleteGroup(groupId);
		return new Quokka(true);
	}
	
	@PostMapping("saveupdate")
	public Quokka saveUpdateMenu(@RequestBody TaskListMenu menu){
		return new Quokka(true, taskListMenuService.saveUpdateTaskListMenu(menu));
	}
	
	@GetMapping("delete")
	public Quokka deleteMenu(long menuId){
		return new Quokka(true, taskListMenuService.deleteMenu(menuId));
	}
	
	@GetMapping("load")
	public Quokka loadMenu(long menuId){
		return new Quokka(true, taskListMenuService.loadMenu(menuId));
	}
	
	@GetMapping("listprocactparts")
	public Quokka listProcActParts(int bizId){
		List<ProcDef> procs = this.feignClientFreeflow.loadProcActParts(bizId);
		return new Quokka(true, procs);
	}
}
